//
//  BookAccount.swift
//  LiteAccount
//
//  Created by 刘伟 on 2017/5/10.
//  Copyright © 2017年 刘伟. All rights reserved.
//

import Foundation


struct AccountModel {
    var id: Int
    var amount: Double
    var note: String
    var date: String
    var detail = [AccountDetailModel]()
    
    init() {
        self.id = 0
        self.amount = 29999.00
        self.note = "我要记录"
        self.date = "2017-05-14"
    }
    
    init(dic: [String: Any]) {
        self.id = dic["id"] as! Int
        self.amount = dic["amount"] as! Double
        self.note = dic["note"] as! String
        self.date = dic["date"] as! String
    }
    
    init(json: JSON) {
        self.id = json["id"].int ?? 0
        self.amount = json["amount"].double ?? 0.00
        self.note = json["note"].string ?? ""
        self.date = json["record_date"].string ?? ""
        if let detailJsonList = json["detail"].array {
            for detailJson in detailJsonList  {
                let detail = AccountDetailModel(json: detailJson)
                self.detail.append(detail)
            }
        }
    }
}

struct AccountDetailModel {
    var accountId: Int
    var categoryId: Int
    var name: String
    var amount: Double
    var date: String
    
    init() {
        self.accountId = 0
        self.categoryId = 0
        self.amount = 0.00
        self.name = ""
        self.date = ""
    }
    
    init(dic: [String: Any]) {
        self.accountId = dic["account_id"] as! Int
        self.categoryId = dic["category_id"] as! Int
        self.name = dic["name"] as! String
        self.amount = dic["amount"] as! Double
        self.date = ""
    }
    
    init(json: JSON){
        self.accountId = json["account_id"].int ?? 0
        self.categoryId = json["category_id"].int ?? 0
        self.name = json["name"].string ?? ""
        self.amount = json["amount"].double ?? 0.00
        self.date = json["record_datetime"].string ?? ""
    }
}
